import { Injectable, inject } from '@angular/core';
import { ElectronService } from '@/service/electron.service';
import { g } from './global-data';


@Injectable({
    providedIn: 'root'
})
export class KmlWriterService {
    electronService = inject(ElectronService);

   async write_kml(node: any) {
        const out_lines: string[] = [];

        out_lines.push('<?xml version="1.0" encoding="UTF-8"?>\n')
        out_lines.push('<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">\n')

        if (node.type === 'folder') {
            await this.write_document(node, out_lines);
        } else if (node.type === 'track_line') {
            await this.write_track_line(node, out_lines);
        } else if (node.type === 'way_point') {
            this.write_way_point(node, out_lines);
        }

        out_lines.push('</kml>\n');
        return out_lines.join('');
    }

    async write_track_line(track_line: any, out_lines: string[]) {
        out_lines.push('    <Placemark>\n');
        out_lines.push('        <name>' + track_line.label + '</name>\n');
        out_lines.push('        <Style>\n');
        out_lines.push('            <LineStyle>\n');
        const color_str = track_line.alpha.toString(16).padStart(2, 0) 
                            + track_line.blue.toString(16).padStart(2, 0) 
                            + track_line.green.toString(16).padStart(2, 0) 
                            + track_line.red.toString(16).padStart(2, 0);
        out_lines.push('                <color>' + color_str + '</color>\n');
        out_lines.push('                <width>' + track_line.width + '</width>\n');
        out_lines.push('            </LineStyle>\n');
        out_lines.push('        </Style>\n');
        if (track_line.has_timestamp) {
            await this.write_points_with_time(track_line, out_lines); 
        } else {
            await this.write_points_without_time(track_line, out_lines);
        }
            
        out_lines.push('    </Placemark>\n');    
    }

    async write_points_with_time(track_line: any, out_lines: string[]) {
        out_lines.push('        <gx:Track>\n');

        let track_line_points = g.track_line_points_cache.get(track_line.uuid);
        if (!track_line_points) {
            track_line_points = await this.electronService.invoke('load-track-data', track_line.uuid);
        }
        if (!!track_line_points) {
            for (const pt of track_line_points) {
                out_lines.push('            <when>' + pt[3] + '</when>\n');
            }
            for (const pt of track_line_points) {
                out_lines.push('            <gx:coord>' + pt[0].toFixed(8) + ' ' + pt[1].toFixed(8) + ' ' + pt[2].toFixed(2) + '</gx:coord>\n');
            }
        }
        out_lines.push('        </gx:Track>\n');
    }

    async write_points_without_time(track_line: any, out_lines: string[]) {
        out_lines.push('        <LineString>\n');
        out_lines.push('            <coordinates>\n');

        let track_line_points = g.track_line_points_cache.get(track_line.uuid);
        if (!track_line_points) {
            track_line_points = await this.electronService.invoke('load-track-data', track_line.uuid);
        }
        if (!!track_line_points) {
            for (const pt of track_line_points) {
                out_lines.push('                ' + pt[0].toFixed(8) + ',' + pt[1].toFixed(8) + ',' + pt[2].toFixed(2) + '\n');
            }
        }

        out_lines.push('            </coordinates>\n');
        out_lines.push('        </LineString>\n');
    }

    write_way_point(wpt: any, out_lines: string[]) {
        out_lines.push('    <Placemark>\n');
        out_lines.push('        <name>' + wpt.label + '</name>\n');
        out_lines.push('        <Point>\n');
        out_lines.push('            <coordinates>' + wpt.lon.toFixed(8) + ',' + wpt.lat.toFixed(8) + ',' + wpt.alt.toFixed(2) + '</coordinates>\n');
        out_lines.push('        </Point>\n');
        if (!!wpt.timestamp) {
            out_lines.push('        <Timestamp>\n');
            out_lines.push('            <when>' + wpt.timestamp + '</when>\n');
            out_lines.push('        </Timestamp>\n');         
        }
        out_lines.push('    </Placemark>\n');
    }

    async write_document(doc: any, out_lines: string[]) {
        out_lines.push('    <Document>\n');
        out_lines.push('        <name>' + doc.label + '</name>\n');

        for (const child of doc.children) {
            if (child.type === 'folder') {
                await this.write_document(child, out_lines);
            } else if (child.type === 'track_line') {
                await this.write_track_line(child, out_lines);
            }else if (child.type === 'way_point') {
                this.write_way_point(child, out_lines);
            }        
        }

        out_lines.push('    </Document>\n');
    }

}
